<?php
class LoginAction extends Action {
    public function index(){
		session('adminInfo', NULL);
		session('sessionRoleInfo', NULL);
		session('sessionAccessList', NULL);
		
		$this->display();
    }
	
	public function login(){
		$username = I('username', '', 'trim,strip_tags');
		$password = I('password', '', 'trim,strip_tags');
		
		if(session('verify') != md5($_POST['verify'])) {
            $this->error('验证码输入有误', U('index'));
        }

		if(empty($username) || empty($password)){
			$this->error('账号或密码有误，请重新输入。');	
		}
		
		$adminInfo = D('Admin')->getByUsername($username);
		$roleInfo = D('Role')->getById($adminInfo['role_id']);
		
		if($adminInfo['status'] == 2 || $roleInfo['status'] == 2){
			$this->error('账号被禁止登录!');	
		}else if($adminInfo['password'] == setPassword($password, $adminInfo['pwd_salt'])){
			session('adminInfo', $adminInfo);
			redirect(U('/Index'));
		}else{
			$this->error('账号或密码有误，请重新输入。');	
		}
	}
	
	public function logout(){
		session('adminInfo', NULL);
		session('sessionRoleInfo', NULL);
		session('sessionAccessList', NULL);
		//redirect(U('/Login'));
		echo '<script>top.location.href = "'.U('/Login').'";</script>';
	}

	public function verify() {
        import("ORG.Util.Captcha");
		ob_end_clean();
		$captcha = new Captcha();
		//$captcha->debug = true;
		$captcha->doimg();
    }
}